
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 2
 * @Author : WangRuoyu 
 * @Date : 2023/5/16 16:05
 */

#include "iostream"

typedef long long ll;

using namespace std;

ll r, y, g, n;

ll addtime(ll res, ll k, ll t) {
    ll total = r + y + g;
    if (k == 1) res += r - t;
    else if (k == 2) res += total - t;
    else if (k == 3) res += r + g - t;
    res = res % total;
    if (res >= 0 && res < r) return r - res;
    else if (res >= r && res < r + g) return 0;
    else return y + r - res + r + g;
}

int main() {
    cin >> r >> y >> g >> n;
    ll res = 0;

    for (ll i = 0; i < n; ++i) {
        ll k, t;
        cin >> k >> t;
        if (k == 0) {
            res += t;
        } else {
            ll tmp = addtime(res, k, t);
            res += tmp;
        }
    }

    cout << res;
    return 0;
}